package com.zhuiYun.project.api.shArea.service.impl;

import com.zhuiYun.project.api.shArea.entiy.ShArea;
import com.zhuiYun.project.api.shArea.mapper.ShAreaMapper;
import com.zhuiYun.project.api.shArea.service.ShAreaService;
import com.zhuiYun.project.api.userAddress.entiy.UserAddress;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @ClassName ShAreaServiceImpl
 * @Description TODO 全国地址
 * @Author 胡亮
 * @Date 2023/04/15 上午 10:06
 **/
@Service
public class ShAreaServiceImpl implements ShAreaService {
    @Autowired
    ShAreaMapper shAreaMapper;


    /**
    * @Author 胡亮
    * @Description Pc全国地址
    * @Date 2023/04/15 上午 10:15
    * @Return []
    */

    @Override
    public CommonResult shAreaAll() {
        //从Redis取出全国地址所用参数
        Object pcAreaMap = RedisUtil.get("pcAreaMap");
        //判断Redis是否有这个参数
        if (pcAreaMap != null && !pcAreaMap.equals("null")) {
            //返回全国地址
            return CommonResult.ok(pcAreaMap);
        }
        //如果Redis没有全国地址所用参数
            else {
            //存储全国地址所用参数map
            HashMap<Object, Object> map = new HashMap<>();
            //搜索省级参数
            Example example = new Example(ShArea.class);
            Example.Criteria criteria = example.createCriteria();
            //条件level（1省级2市级3县级）
            criteria.andEqualTo("level", 1);
            //排序条件id（升序）
            example.setOrderByClause("id asc");
            List<ShArea> shAreas = shAreaMapper.selectByExample(example);
            for (ShArea s : shAreas) {
                //搜索市级参数
                Example example1 = new Example(ShArea.class);
                Example.Criteria criteria1 = example1.createCriteria();
                //条件pid（父id）
                criteria1.andEqualTo("pid", s.getId());
                //排序条件first（首字母升序）
                example1.setOrderByClause("first asc");
                List<ShArea> shAreas1 = shAreaMapper.selectByExample(example1);
                for (ShArea s1 : shAreas1) {
                    //搜索县级参数
                    Example example2 = new Example(ShArea.class);
                    Example.Criteria criteria2 = example2.createCriteria();
                    //条件pid（父id）
                    criteria2.andEqualTo("pid", s1.getId());
                    //排序条件first（首字母升序）
                    example2.setOrderByClause("first asc");
                    List<ShArea> shAreas2 = shAreaMapper.selectByExample(example2);
                    //县级参数赋值
                    s1.setShArea(shAreas2);
                }
                //市级参数赋值
                s.setShArea(shAreas1);
            }
            //全国地址(不带级别列表)
            List<ShArea> shAreasName = shAreaMapper.selectAll();
            map.put("shAreaList", shAreas);
            map.put("shAreaNameList", shAreasName);
            //存储到Redis中
            RedisUtil.set("pcAreaMap", map);
            return CommonResult.ok(map);
        }
    }

    /**
     * @Author gpd
     * @Description 查询所有地址信息
     * @Date 2023-04-15 上午 10:20
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public CommonResult getAreaList() {
        Object areaListMap = RedisUtil.get("areaListMap");
        if (areaListMap != null && !areaListMap.equals("null")) {
            return CommonResult.ok(areaListMap);
        } else {
            ShArea areaEntity = new ShArea();
            HashMap<Object, Object> map = new HashMap<>();
            ArrayList<Object> provinceData = new ArrayList<>();
            ArrayList<Object> areaData = new ArrayList<>();
            ArrayList<Object> countyData = new ArrayList<>();
            areaEntity.setPid(0);
            List<ShArea> provinceList = shAreaMapper.select(areaEntity);
            provinceData.add(provinceList);
            Integer id = provinceList.get(0).getId();
            areaEntity.setPid(id);
            List<ShArea> provincePidOneIndexList = shAreaMapper.select(areaEntity);
            provinceData.add(provincePidOneIndexList);
            areaEntity.setPid(provincePidOneIndexList.get(0).getId());
            List<ShArea> areaPidOneIndexList = shAreaMapper.select(areaEntity);
            provinceData.add(areaPidOneIndexList);
            for (ShArea province : provinceList) {
                List<ShArea> areaEntities = shAreaMapper.selectArea(province.getId());
                for (ShArea area : areaEntities) {
                    List<ShArea> countyEntities = shAreaMapper.selectArea(area.getId());
                    countyData.add(countyEntities);
                }
                areaData.add(areaEntities);
            }
            map.put("provinceData",provinceData);
            map.put("areaData", areaData);
            map.put("countyData", countyData);
            RedisUtil.set("areaListMap", map);
            return CommonResult.ok(map);
        }
    }
}
